package m.stock.v1;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Collection;
import java.util.List;

/**
 * Created by mao on 2016/8/3.
 */
public class Launch {
    private static Logger logger = Logger.getLogger(Launch.class);
    public static void main(String[] args) {

        try {
            logger.debug(args[0]);
            String candidateFileLocation = args[0];
            int trendLength = Integer.parseInt(args[1]);
            File candidateDir = new File(candidateFileLocation);
            String parentDir = candidateDir.getParent();
            logger.debug("parentDir:"+parentDir);
            String intermediateDataLocation = parentDir+File.separator+"intermediateData";
            String trendDataLocation = parentDir+File.separator+"trendData";
            logger.debug("intermediateDataLocation:"+intermediateDataLocation);
            File intermediateDataDir = new File(intermediateDataLocation);
            if(intermediateDataDir.exists()){
                FileUtils.forceDelete(intermediateDataDir);
            }
            FileUtils.forceMkdir(intermediateDataDir);

            Collection<File> candidateFileCollection = FileUtils.listFiles(candidateDir, new String[]{"xls"}, false);
            logger.debug(candidateFileCollection.size());
            for(File candidateFile:candidateFileCollection){
                logger.debug("processing:"+candidateFile.getName());
                List<StockRecord> records = Parser.parse(candidateFile);
                RecordDistributor.distribute(records,intermediateDataLocation);
            }
            TrendPersister.persist(intermediateDataLocation,trendDataLocation,trendLength);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }
}
